﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;

/// <summary>
///Order 的摘要说明
/// </summary>
public partial class Order
{
    public enum orderstatue { 未成功 = 1, 成功未支付, 成功且支付, 支付后发货中, 货收到 }
    public static Order Get(int id)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.Order.FirstOrDefault(row => row.ID == id);
        }
    }

    public static Order GetByOrderID(string OrderID)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.Order.FirstOrDefault(row => row.OrderID == OrderID);
        }
    }


    public static List<Order> GetAll()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.Order.ToList();
        }
    }

    public static List<Order> GetAllByUserID_OrderStatus(int UserID, int OrderStatus)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from o in context.Order
                        select o;
            if (UserID != 0)
                query = query.Where(row => row.AddUserID == UserID);
            if (OrderStatus != 0)
                query = query.Where(row => row.Status == OrderStatus);
            return query.OrderByDescending(row => row.AddTime).ToList();

        }
    }

    public static List<Order> GetAllSuccessByUserID(int UserID)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from o in context.Order
                        where o.Status != (int)orderstatue.未成功
                        select o;

            if (UserID != 0)
                query = query.Where(row => row.AddUserID == UserID);
            return query.OrderByDescending(row => row.AddTime).ToList();
        }
    }


    public void Add()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            context.Order.InsertOnSubmit(this);
            context.SubmitChanges();
        }
    }

    public static void AddList(List<Order> orders)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            context.Order.InsertAllOnSubmit(orders);
            context.SubmitChanges();
        }
    }

    public void Update()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            this.GetType().GetMethod("Detach").Invoke(this, null);
            var tbl = context.GetTable<Order>();
            tbl.Attach(this);
            context.Refresh(RefreshMode.KeepCurrentValues, this);
            context.SubmitChanges();
        }
    }

    public static void Delete(int id)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var objItem = context.Order.FirstOrDefault(row => row.ID==id );
            if (objItem == null)
                return;
            context.Order.DeleteOnSubmit(objItem);
            context.SubmitChanges();
        }
    }
}